﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Converters;
//using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace EbGameBaseConfig
{
    public class EbGameBaseConfigMgr
    {
        static private EbGameBaseConfigMgr _instance = null;

        public string jonsFilePath;
        public JToken fileJTkon;


        static public EbGameBaseConfigMgr Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new EbGameBaseConfigMgr();
                }
                return _instance;
            }
        }

        public void Init(string jsonfile)
        {
            try
            {
                jonsFilePath = jsonfile;
                if (!File.Exists(jsonfile))
                {
//                    Debug.LogErrorFormat("{0} is no exists", jsonfile);
                }

                using (System.IO.StreamReader file = System.IO.File.OpenText(jsonfile))
                {
                    using (JsonTextReader reader = new JsonTextReader(file))
                    {

                        fileJTkon = JToken.ReadFrom(reader);
                        file.Close();
                    }
                }

            }

            catch (Exception e)
            {
//                Debug.LogError(e.Message);
                throw e;
            }
        }

        public bool GetValue(string key, out string value)
        {
            if (fileJTkon == null)
            {
                value = string.Empty;
 //               Debug.LogError("fileJTkon is null");
                return false;
            }
            JToken rootJT = fileJTkon["AppliactionInfo"];
            if (rootJT != null)
            {
                JObject rootJO = rootJT.ToObject<JObject>();
                JToken retJT;
                if (rootJO.TryGetValue(key, out retJT))
                {
                    value = retJT.ToString();
                    return true;
                }
            }

            value = string.Empty;
            return false;
        }


        public bool SetValue(string key, string value)
        {
            if (fileJTkon == null)
            {
                value = string.Empty;
//                Debug.LogError("fileJTkon is null");
                return false;
            }

            JToken rootJT = fileJTkon["AppliactionInfo"];
            if (rootJT != null)
            {
                JObject rootJO = (JObject)rootJT;
                JToken retJT;
                if (rootJO.TryGetValue(key, out retJT))
                {
                    retJT = value;
                    return true;
                }
            }
            return false;

        }

        public void SaveFile()
        {

            if (!File.Exists(jonsFilePath) || fileJTkon==null)
            {
//                Debug.LogErrorFormat("{0} is no exists");
            }

            using (System.IO.StreamWriter file = new StreamWriter(jonsFilePath))
            {
                using (JsonTextWriter writer = new JsonTextWriter(file))
                {
                    string jsontxt = fileJTkon.ToString();
                    writer.WriteComment(jsontxt);
                }
            }
        }
    }
}
